/*
 * GLComponentDelegate.cpp  0.4.0 / Nov 28, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "GLComponentDelegate.h"

#include "QGLWidgetAdapter.h"

// GLComponentDelegate :: Protected Constructors. ------------------------------

/*!
 * \brief Constructs new GLComponentDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the GLComponent java object 
 */
GLComponentDelegate::GLComponentDelegate(JNIEnv *env, jobject obj)
  : ComponentDelegate(env, obj, new QGLWidgetAdapter(this))
{
  setDelegatePointer(reinterpret_cast<jlong>(this));
}

// GLComponentDelegate :: Native Calls. ---------------------------------------

/*!
 * \brief Updates GL.
 *
 * \param env the JNIEnv pointer 
 */
void GLComponentDelegate::updateGL(JNIEnv *env)
{
  Q_ASSERT(env);
  SWING_REENTRANT(updateGL, SWING_ENV_ARG)
  qGLWidget()->updateGL();
}

// GLComponentDelegate :: Static Native Calls. ---------------------------------

/*!
 * \brief Initializes GLComponentDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the GLComponent java class
 *
 * \exception FatalError if failed
 */
void GLComponentDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_createGLMID);
  if (!INIT_UMID(createGL, cls, "()J") || !INIT_MID(initializeGL, cls, "()V") ||
      !INIT_MID(resizeGL, cls, "(II)V") || !INIT_MID(paintGL, cls, "()V")) {
    FATAL_LOGGED_ERROR("GLComponentDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object.
 *
 * \param env the JNIEnv pointer 
 * \param obj the Swing java object
 * 
 * \return new instance of the delegate object
 */
jlong GLComponentDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new GLComponentDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, sm_createGLMID);
}

// GLComponentDelegate :: Private Static Data Members. -------------------------

jmethodID GLComponentDelegate::sm_createGLMID = 0;
jmethodID GLComponentDelegate::sm_initializeGLMID = 0;
jmethodID GLComponentDelegate::sm_resizeGLMID = 0;
jmethodID GLComponentDelegate::sm_paintGLMID = 0;
